import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '@/views/home/index.vue'
import Login from '@/views/login/index.vue'
import Error from '@/views/404/index.vue'
import Screen from '@/views/screen/index.vue'
import Layout from '@/layout/index.vue'
import Role from '@/views/acl/role/index.vue'
import User from '@/views/acl/user/index.vue'
import Permission from '@/views/acl/permission/index.vue'
import Attribute from '@/views/product/attribute/index.vue'
import Brand from '@/views/product/brand/index.vue'
import SKU from '@/views/product/SKU/index.vue'
import SPU from '@/views/product/SPU/index.vue'
//@ts-ignore
import nprogress from 'nprogress'
import { Message } from '@/utils/message'
import setting from '@/setting'

const routes = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: Login,
      meta: {
        title: '登录',
      },
    },
    {
      path: '/',
      name: 'layout',
      redirect: '/home',
      component: Layout,
      meta: {
        title: '首页',
      },
      children: [
        {
          path: '/home',
          name: 'home',
          component: Home,
          meta: {
            title: '主页',
          },
        },
      ],
    },
    {
      path: '/acl/',
      name: 'acl',
      component: Layout,
      redirect: '/acl/user',
      meta: {
        title: '权限管理',
      },
      children: [
        {
          path: '/acl/role',
          name: 'role',
          component: Role,
          meta: {
            title: '用户管理',
          },
        },
        {
          path: '/acl/user',
          name: 'user',
          component: User,
          meta: {
            title: '角色管理',
          },
        },
        {
          path: '/acl/permission',
          name: 'permission',
          component: Permission,
          meta: {
            title: '菜单管理',
          },
        },
      ],
    },
    {
      path: '/product/',
      name: 'product',
      component: Layout,
      redirect: '/product/attribute',
      meta: {
        title: '商品管理',
      },
      children: [
        {
          path: '/product/attribute',
          name: 'attribute',
          component: Attribute,
          meta: {
            title: '商品属性',
          },
        },
        {
          path: '/product/brand',
          name: 'brand',
          component: Brand,
          meta: {
            title: '商品品牌',
          },
        },
        {
          path: '/product/SKU',
          name: 'SKU',
          component: SKU,
          meta: {
            title: 'SKU',
          },
        },
        {
          path: '/product/SPU',
          name: 'SPU',
          component: SPU,
          meta: {
            title: 'SPU',
          },
        },
      ],
    },
    {
      path: '/404',
      name: '404',
      component: Error,
    },

    {
      path: '/screen',
      name: 'screen',
      component: Screen,
    },

    {
      path: '/:pathMatch(.*)*',
      redirect: '/404',
      name: 'any',
    },
  ],
})

routes.beforeEach((to, from, next) => {
  let token = localStorage.getItem('TOKEN')
  nprogress.start()
  if (to.path === '/login') {
    if (token) {
      return next(from.path)
    }
    // Message('error', '登录信息失效，请重新登录', 2000)
    return next()
  } else {
    if (!token) {
      Message('error', '请先登录', 2000)
      return next({
        path: '/login',
        query: {
          redirect: from.path,
        },
      })
    }
  }
  //放行
  next()
})

routes.afterEach((to, _from, _failure) => {
  document.title = `${setting.title} - ${to.meta.title}`
  nprogress.done()
})

export default routes
